package gspushmq.v1.save;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.GGMongoOperator;
import gspushmq.model.ConsumerWays;
import gspushmq.model.Data;
import gspushmq.model.DataMessage;
import gspushmq.model.MessageType;
import gspushmq.util.DateUtil;
import gspushmq.util.GGLogger;
import gspushmq.util.MongoUtil;
import gspushmq.v1.produce.GGDataStorage;
import gspushmq.v1.subscribe.GGSubscribe;
import gspushmq.v1.thread.GGThreadPool;

/**
 *
 * 记录生产出来的消息
 * @version 1.0
 * @since JDK1.7
 * @author yutao
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2018年11月6日下午5:08:00
 */
public class SaveMassRecord {

	/**
	 * 
	 * 方法描述 保存消息
	 *
	 * @param data
	 * 
	 * @author lisl
	 * @date 2019年1月16日 下午9:18:56
	 */
	public static void save(Data data) {
		try {
			if(data==null){
				GGLogger.warn(SaveMassRecord.class, "【data】不能为null");
				return;
			}
			String uuid = data.getUuid();
			if(StringUtils.isBlank(uuid)) {
				GGLogger.warn(SaveMassRecord.class, "【uuid】不能为null");
				return;
			}
			BasicDBObject fieldMap = new BasicDBObject();
			BeanInfo beanInfo;
			beanInfo = Introspector.getBeanInfo(Data.class, Object.class);
			 
			//获取所有的属性描述器 
			PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); 
			for(PropertyDescriptor pd:pds){ 
				String key = pd.getName(); 
				Method getter = pd.getReadMethod(); 
				Object obj = getter.invoke(data);
				if("content".equals(key) && !(obj instanceof String )) {
					obj = JSON.toJSONString(obj);
				}
				fieldMap.put(key, obj); 
			}
			if(fieldMap.isEmpty()){
				return;
			}
			fieldMap.append("insert_time", new Date());
			
			DBCollection messRecord = MongoUtil.getFtAccountBehaviorCollection("cbs_msg_storage_record");
			BasicDBObject match = GGMongoOperator.newDBObject(messRecord);
			match.putAll(fieldMap);
			messRecord.insert(match);
			
		} catch (Exception e) {
			GGLogger.error(SaveMassRecord.class, "---保存消息---出错---" + e.toString());
			e.printStackTrace();
		}
	}
	
	/**
	  *  不需要经过逻辑处理的
	 * @param
	 * @param pushList
	 * @param title
	 * @author yutao
	 * @date 2018年11月21日下午1:15:23
	 */
	public static void handlerData(MessageType messType, Object pushList, String title) {
		DataMessage dataMessage =  new DataMessage();
		String mqSubType = messType.getMq_sub_type();
		if("tags".equals(mqSubType)){
			dataMessage.setCrowdType(ConsumerWays.ALL.getWay());
			dataMessage.setCrowdValue("");
			dataMessage.setExpireDate(DateUtil.getEndOfDay());
		}
		dataMessage.setUserType(messType.getUser_type());
		dataMessage.setMqType(messType.getMq_type());
		dataMessage.setMqSubType(messType.getMq_sub_type());
		dataMessage.setTitle(title);
		dataMessage.setContent(pushList);
		GGDataStorage.MessageMap.put(messType, dataMessage);
		GGSubscribe.activeQueue.add(messType);
		save(dataMessage);
	}
}
